const crypto = require('crypto');

const algorithm = 'aes-128-ecb';
const key = '1122334455667788'; // KEYSIZE 128, KEY 是 16位
const iv = null; // ECB 模式不需要 IV

/**
 * 加密
 * @param {string} text 原文
 */
function encrypt(text) {
  const cipher = crypto.createCipheriv(algorithm, key, iv);
  return Buffer.concat([cipher.update(text), cipher.final()]).toString('hex');
}
/**
 * 解密
 * @param {string} text 密文
 */
function decrypt(text) {
  var decipher = crypto.createDecipheriv(algorithm, key, iv);
  return Buffer.concat([
    decipher.update(text, 'hex'),
    decipher.final(),
  ]).toString();
}

const rawplain = 'Hello World!';
const ciphtext = encrypt(rawplain);
const plaintext = decrypt(ciphtext);
console.log('原文', rawplain);
console.log('加密的密文', ciphtext);
console.log('解密的明文', plaintext);

// 原文 Hello World!
// 加密的密文 1d604f5a0cc944db496c7bf8e97aeebb
// 解密的明文 Hello World!
